<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/4/22
 * Time: 20:19
 */

namespace common\logics\flowwater;

use common\widgets\Tool;
use Yii;
use yii\base\BaseObject;

class FlowWaterLogic extends BaseObject implements IFlowWater {
	/** @var \common\sources\read\buyuser\BuyUserAccess $RBuyUserAccess * */
	private $RBuyUserAccess;
	/** @var \common\sources\read\buyuser\BuyUserAcountAccess $RBuyUserAcountAccess ** */
	private $RBuyUserAcountAccess;
	/** @var \common\sources\read\businessaccount\BusinessAccountAccess $RBusinessAccountAccess * */
	private $RBusinessAccountAccess;
	/** @var \common\sources\read\buydepositrecord\BuyDepositRecordAccess $RBuyDepositRecordAccess * */
	private $RBuyDepositRecordAccess;
	/** @var \common\sources\read\buypepperrecord\BuyPepperRecordAccess $RBuyPepperRecordAccess * */
	private $RBuyPepperRecordAccess;
	/** @var \common\sources\read\businesspepperrecord\BusinessPepperRecordAccess $RBusinessPepperRecordAccess * */
	private $RBusinessPepperRecordAccess;
	/** @var \common\sources\read\businessdepositrecord\BusinessDepositRecordAccess $RBusinessDepositRecordAccess * */
	private $RBusinessDepositRecordAccess;
	/** @var \common\sources\read\adminuser\AdminUserAccess $RAdminUserAccess ** */
	private $RAdminUserAccess;

	public function init() {
		$this->RBuyUserAccess               = Yii::$container->get("RBuyUserAccess");
		$this->RBuyUserAcountAccess         = Yii::$container->get("RBuyUserAcountAccess");
		$this->RBusinessAccountAccess       = Yii::$container->get("RBusinessAccountAccess");
		$this->RBuyPepperRecordAccess       = Yii::$container->get("RBuyPepperRecordAccess");
		$this->RBuyDepositRecordAccess      = Yii::$container->get("RBuyDepositRecordAccess");
		$this->RBusinessPepperRecordAccess  = Yii::$container->get("RBusinessPepperRecordAccess");
		$this->RBusinessDepositRecordAccess = Yii::$container->get("RBusinessDepositRecordAccess");
		$this->RAdminUserAccess             = Yii::$container->get("RAdminUserAccess");

		parent::init();
	}

	/**
	 * getBuyMessage 账户明细
	 *
	 * @param $data
	 *
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/7
	 * @return false|string
	 */
	public function getBuyMessage($data) {
		if (empty($data['buy_id'])) {
			return Tool::returnJson([], 401, "用户id不能为空");
		}
		if (empty($data['type'])) {
			return Tool::returnJson([], 402, "类型不能为空");
		}

		//获取用户信息
		$buyUser = $this->RBuyUserAccess->getBuyUserInfo($data['buy_id']);
		if (empty($buyUser)) {
			return Tool::returnJson([], 403, "账号不存在");
		}
		$count       = 0;
		$list        = [];
		$userAccount = [];

		// 检索条件组装
		$where = ($buyUser['role'] == 1) ? ' buy_id =' . $data['buy_id'] : ' bus_id = ' . $data['buy_id'];
		if (!empty($data['start_time'])) {
			$where .= ' and create_time >=' . $data['start_time'];
		}
		if (!empty($data['end_time'])) {
			$where .= ' and create_time  <=' . $data['end_time'];
		}

		if (!empty($data['value'])) {
			if ($data['query_by'] == 1) {
				//子任务编号
				$where .= ' and task_order_num = "' . $data['value'] . '"';
			} elseif ($data['query_by'] == 2) {
				//主任务编号
				$where .= ' and task_id = "' . $data['value'] . '"';
			} elseif ($data['query_by'] == 3) {
				//支付编号
				$where .= ' and order_id = "' . $data['value'] . '"';
			}
		}


		//判断是商家还是买手
		if ($buyUser['role'] == 1) {
			$userAccount = $this->RBuyUserAcountAccess->getBuyUserAccount($data['buy_id']);
			if (empty($userAccount)) {
				$userAccount = [
					'remain_funds'   => 0,//本金余额
					'remail_golds'   => 0,//花椒余额
					'freezing_funds' => 0,//冻结本金
					'freezing_golds' => 0,//冻结花椒
					'income_golds'   => 0//花椒收益
				];
			}

			if ($data['type'] == 1) {//1. 押金
				if ($this->RBuyDepositRecordAccess->getBuyDepositRecordCount($where) > 0) {
					$list = $this->RBuyDepositRecordAccess->getBuyDepositRecordList($data,$where);
					foreach ($list as &$item) {
						$item['create_time'] = isset($item['create_time']) ? date('Y-m-d H:i:s', $item['create_time']) : '';

						//管理员信息
						$adminInfo          = $this->RAdminUserAccess->getDetial($item['operatorId']);
						$item['operatorId'] = isset($adminInfo['username']) ? $adminInfo['username'] : $item['operatorId'];
					}
					unset($item);
				}
			} else if ($data['type'] == 2) {//2. 花椒
				if ($this->RBuyPepperRecordAccess->getPepperCout($where) > 0) {
					$list = $this->RBuyPepperRecordAccess->getPepperList($data,$where);
					foreach ($list as &$item) {
						$item['create_time'] = isset($item['create_time']) ? date('Y-m-d H:i:s', $item['create_time']) : '';
					}
					unset($item);
				}
			}

		} else if ($buyUser['role'] == 2) {//商家
			$userAccount = $this->RBusinessAccountAccess->getBusinessAccount($data['buy_id']);
			if (empty($userAccount)) {
				$userAccount = [
					'total_deposit'     => 0,//总押金
					'ava_deposit'      => 0,//可用押金
					'ava_pepper'       => 0,//可用花椒
					'freezing_deposit' => 0,//冻结押金
					'freezing_golds'   => 0,//任务冻结花椒
					'cash_deposit'     => 0//待审核提现
				];
			} else {
				unset($userAccount['bus_id'], $userAccount['create_time']);
			}


			//日志押金余额
			$userAccount['log_deposit_balance'] = 0;
			//日志押金冻结余额
			$userAccount['log_deposit_freezing'] = 0;
			//日志花椒余额
			$userAccount['log_pepper_balance'] = 0;
			//日志花椒冻结余额
			$userAccount['log_pepper_freezing'] = 0;


			if ($data['type'] == 1) {//1. 押金
				$count = $this->RBusinessDepositRecordAccess->getDepositRecordCount($where);
				if($count<$data['limit'])
				{
				    $data['page_num'] = 1;
                }
				if ($count > 0) {
					$list = $this->RBusinessDepositRecordAccess->getDepositRecordList($data,$where);
					$type = ['1' => '充值', '2' => '扣减', 3 => '任务返还', 4 => '冻结', '5' => '解冻', '6' => '扣除冻结'];
					foreach ($list as &$item) {
						$item['create_time'] = isset($item['create_time']) ? date('Y-m-d H:i:s', $item['create_time']) : '';
						$item['end_time']    = isset($item['end_time']) ? date('Y-m-d H:i:s', $item['end_time']) : '';
						$item['type']        = isset($type[$item['type']]) ? $type[$item['type']] : $item['type'];

						//管理员信息
						$adminInfo             = $this->RAdminUserAccess->getDetial($item['end_user']);
						$item['end_user'] = isset($adminInfo['username']) ? $adminInfo['username'] : $item['end_user'];
						//管理员信息
						$adminInfo             = $this->RAdminUserAccess->getDetial($item['operatorId']);
						$item['operatorId'] = isset($adminInfo['username']) ? $adminInfo['username'] : $item['operatorId'];
						//管理员信息
						$adminInfo             = $this->RAdminUserAccess->getDetial($item['auditor_user']);
						$item['auditor_user'] = isset($adminInfo['username']) ? $adminInfo['username'] : $item['auditor_user'];
					}
					unset($item);
				}
			} else if ($data['type'] == 2) {//2. 花椒
				$count = $this->RBusinessPepperRecordAccess->getPepperRecordCount($where);
				if($count<$data['limit'])
				{
				    $data['page_num'] =1;
                }
				if ($count > 0) {
					$list = $this->RBusinessPepperRecordAccess->getPepperRecordList($data,$where);
					$type = ['1' => '充值', '2' => '扣减', 3 => '返还', 4 => '冻结', '5' => '解冻', '6' => '扣除冻结'];
					foreach ($list as &$item) {
						$item['create_time'] = isset($item['create_time']) ? date('Y-m-d H:i:s', $item['create_time']) : '';
						$item['type']        = isset($type[$item['type']]) ? $type[$item['type']] : $item['type'];
						$item['serial_num']  = '';

						//管理员信息
						$adminInfo          = $this->RAdminUserAccess->getDetial($item['operatorId']);
						$item['operatorId'] = isset($adminInfo['username']) ? $adminInfo['username'] : $item['operatorId'];
					}
					unset($item);
				}
			}
		}

		return Tool::returnJson([
			"list"        => $list,
			"count"       => $count,
			"userAccount" => $userAccount,
			"role"        => $buyUser['role'],
			"page_num"    => $data['page_num'],
			"limit"       => $data['limit'],
		], 200, "成功");
	}
}